package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.po.*;
import com.cskaoyan.app.mapper.MarketAPIMapper;
import com.cskaoyan.app.mapper.MarketAdminMapper;
import com.cskaoyan.app.mapper.MarketPermissionMapper;
import com.cskaoyan.app.mapper.MarketRoleMapper;
import com.cskaoyan.app.service.AdminAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminAuthServiceImpl implements AdminAuthService {

    @Autowired
    private MarketAdminMapper adminMapper;

    @Autowired
    private MarketRoleMapper roleMapper;

    @Autowired
    private MarketPermissionMapper permissionMapper;

    @Autowired
    private MarketAPIMapper apiMapper;

    @Override
    public String getAdminPass(String username) {
        MarketAdminExample adminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username).andDeletedEqualTo(false);
        List<MarketAdmin> admins = adminMapper.selectByExample(adminExample);
        return admins.stream().findFirst().map(MarketAdmin::getPassword).orElse("");
    }

    @Override
    public AdminInfoBean getInfoBean(String username) {
        AdminInfoBean infoBean = new AdminInfoBean();
        MarketAdminExample adminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username).andDeletedEqualTo(false);
        List<MarketAdmin> admins = adminMapper.selectByExample(adminExample);
        MarketAdmin admin = admins.stream().findFirst().orElse(new MarketAdmin());
        infoBean.setNickName(admin.getUsername());
        String avatar = admin.getAvatar();
        if (avatar == null) {
            infoBean.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        }
        infoBean.setAvatar(avatar);
        return infoBean;
    }

    @Override
    public void updateLogin(String username, String host, Date lastLogin) {
        MarketAdmin record = new MarketAdmin();
        record.setLastLoginIp(host);
        record.setLastLoginTime(lastLogin);
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        adminMapper.updateByExampleSelective(record, example);
    }

    @Override
    public List<String> getAdminRoles(String username) {
        Integer[] roleIds = getRoleIds(username);
        MarketRoleExample roleExample = new MarketRoleExample();
        MarketRoleExample.Criteria roleCriteria = roleExample.createCriteria();
        roleCriteria.andIdIn(Arrays.asList(roleIds))
                .andEnabledEqualTo(true).andDeletedEqualTo(false);
        List<MarketRole> marketRoles = roleMapper.selectByExample(roleExample);
        return marketRoles.stream().map(MarketRole::getName).collect(Collectors.toList());
    }

    @Override
    public void changePass(String username, String newPass) {
        MarketAdminExample adminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username).andDeletedEqualTo(false);
        MarketAdmin record = new MarketAdmin();
        record.setPassword(newPass);
        adminMapper.updateByExampleSelective(record, adminExample);
    }

    @Override
    public List<String> getAdminPerm(String username) {
        Integer[] roleIds = getRoleIds(username);
        MarketPermissionExample example = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdIn(Arrays.asList(roleIds)).andDeletedEqualTo(false);
        example.setDistinct(true);
        List<String> perms = permissionMapper.selectByExample(example).stream()
                .map(MarketPermission::getPermission)
                .collect(Collectors.toList());
        List<String> apis = apiMapper.selectApiOfPerms(perms);
        if (perms.contains("*")) {
            apis.add("*");
        }
        return apis;
    }

    private Integer[] getRoleIds(String username) {
        MarketAdminExample adminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<MarketAdmin> admins = adminMapper.selectByExample(adminExample);
        return admins.stream().findFirst().orElse(new MarketAdmin())
                .getRoleIds();
    }
}
